

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JOptionPane;

public class Source extends Node_design {
	Socket client = new Socket();
	static int count;
	 RouterDetails r;

	public Source() {
		super("sender");

	}
@Override
public void jButton2_actionPerformed(ActionEvent e) throws SQLException {
SimpleTableExample.showTable();
}

	@Override
	public void jButton1_actionPerformed(ActionEvent e) {
		System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
		FileDialog fd = new FileDialog(this, "Open", FileDialog.LOAD);
		fd.show();
		jTextField2.setText(fd.getDirectory() + fd.getFile());
		try(BufferedReader br = new BufferedReader(new FileReader(
				fd.getDirectory() + fd.getFile()))) 
				{

			String sCurrentLine;
			StringBuilder message = new StringBuilder();
			while ((sCurrentLine = br.readLine()) != null) {
				message.append(sCurrentLine);
				message.append("\n");
				System.out.println(sCurrentLine);
			}

			jTextArea1.setText(message.toString());

		} 
		catch (IOException e1) {
			e1.printStackTrace();
		}
		// try
		// {
		// s="";
		// int b;
		// FileDialog fd=new FileDialog(this,"Open",FileDialog.LOAD);
		// fd.show();
		// FileInputStream fis=new
		// FileInputStream(fd.getDirectory()+fd.getFile());
		// jTextField2.setText(fd.getDirectory()+fd.getFile());
		//
		// while((b=fis.read())!=-1)
		// {
		// message+=(char)b;
		// }
		// jTextArea1.setText(message);
		//
		// }
		// catch(Exception r)
		// {
		// r.printStackTrace();
		// }

	}

	@Override
	public void jButton5_actionPerformed(ActionEvent e) {

		if (jTextArea1.getText().length() == 0) {
			System.out.println("nothing selected in text area");
			JOptionPane.showInternalMessageDialog(contentPane,
					"plese enter some messages");
		} else {
			try {
				count++;

				r = getRouterDetails();
				System.out.println(r.getSource_addr()+","+r.getPort_number());
				client = new Socket(r.getSource_addr(), r.getPort_number());
				SourceThread st = new SourceThread(client, this);
				Thread t = new Thread(st);
				t.start();
				System.out.println("send button  is pressed");
				JOptionPane
						.showInternalMessageDialog(
								contentPane,
								"Acknowledgement received from router "
										+ r.getSource_addr() + ":"
										+ r.getPort_number());
			}

			catch (ConnectException e1) {
				JOptionPane.showInternalMessageDialog(contentPane,
						"selected router is down"+r.getPort_number()+""+r.getSource_addr());
				Queries.update("delete from router where port="+r.getPort_number());
				Queries.update("delete from utility where port="+r.getPort_number());
				e1.printStackTrace();
			} catch (UnknownHostException e1) {
				JOptionPane.showInternalMessageDialog(contentPane,
						"selected router is down");
				Queries.update("delete from router where port="+r.getPort_number());
				Queries.update("delete from utility where port="+r.getPort_number());
				e1.printStackTrace();
			} catch (IOException e1) {

				e1.printStackTrace();
			}
		}

	}

	private RouterDetails getRouterDetails() {

		RouterDetails router = new RouterDetails();
		try {
			if (count > 5) {
				ResultSet r = Queries
						.select("select port,ipaddr from utility where duration=(select MAX(duration) from utility)");
				r.next();
				router.setPort_number(r.getInt("port"));
				router.setSource_addr(r.getString("ipaddr"));
			} 
			else {
				int random = (int) (Math.random() * 5);
				ResultSet r = Queries.select("select port,ipaddr from router");
				System.out.println("Random routing no is=" + random);

				while (random >= 0) 
				{
					r.next();
					if (r.isLast())
					break;
					random--;
				}
				router.setPort_number(r.getInt("port"));
				router.setSource_addr(r.getString("ipaddr"));
				//r.first();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return router;
	}

//	public synchronized  void updateTime(long duration) {
//
////		String query = "update router set duration=" + duration
////				+ " where port=" + r.getPort_number() + " and ipaddr=\""
////				+ r.getSource_addr() + "\"";
//		
//		String query="insert into utility values("+r.getPort_number()+",\""+r.getSource_addr()+"\","+duration+")"; 
//		System.out.println(query);
//		Queries.update(query);
//	}
	
	

	public static void main(String args[]) {
		Source s = new Source();
	}

}
